package io;

import java.io.*;

class Book implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private transient String author;
    //
    public Book(){}

    public Book(int id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

public class SerializeableDemo1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1.构建Book对象
        Book book = new Book(1, "corejava", "Mike");
        //2.将对象序列化到指定文件
        //2.1构建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(
                                        new FileOutputStream("obj.data"));
        //2.2将book对象写入到文件（序列化）
        oos.writeObject(book);
        System.out.println("序列化OK");
        //2.3释放资源
        oos.close();
        //3.将文件中的内容进行反序列化
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.data"));
        //
        Object readObject = ois.readObject();
        System.out.println(readObject);
        //
        ois.close();
    }
}
